/**
 * Created by ZFL on 2017/11/3.
 */
var vm=new Vue({
    el:'#scanPay',
    data:{
        totalPayMoney:'',
        discountRate:0,    //优惠率
    }

})
Vue.filter('discount',function(value){
    return fmoneyStr(value*(100-vm.discountRate)/100)
})


function PageInit(){
    discount();    //获取商家优惠
    if($.getStorage('xmOpenid')){
        $.updataInfo($.getStorage('xmOpenid'))
    }
    //$.delStorage('xm_uData');
    //$.GetWXCode();
    //        if(!JSON.parse($.getStorage('xm_uData')).userName) {
    //            bindPhone();
    //
    //        }else{
    //            if (JSON.parse($.getStorage('xm_uData')).userName.length !== 11) {
    //                bindPhone();
    //            }
    //        }





}
var val='';
var totalPayDOM = $(".totalPayMoney");
var totalPayDOM = $(".totalPayMoney");
var discountPay=0;


$(".offerPay_number_table a").on("touchstart", function (e) {
    e.preventDefault();
    if($(this).hasClass('offerPay_number_delete')||$(this).hasClass('offer_pay_2')){
        var number='';
    }else{
        var number=$(this).html();
    }

    val = $(".totalPayMoney").val();
    if ($(this).hasClass("offer_pay_2")) {
        discountPay=fmoneyStr(vm.totalPayMoney*(100-vm.discountRate)/100);
        if(val==''){
            YDUI.dialog.toast('请输入金额',1000)
        }
        goBalance();
        //return;
    }


    //小数点只能输一次
    if ($(this).html() === "." && vm.totalPayMoney.indexOf(".") > -1) {
        return;
    }
    // 删除输入的数字
    if ($(this).hasClass("offerPay_number_delete")) {
        vm.totalPayMoney = vm.totalPayMoney.slice(0, val.length - 1);
        totalPayDOM.val(val);
        $(".totalPayMoney_gb_money").html(val);
        //长按删除全部数字
        //timer = setInterval(function () {
        //    totalPayDOM.val("");
        //    $(".totalPayMoney_gb_money").html("");
        //    //getFinalPay(0);
        //}, 1500)
    } else {
        val = val + $(this).html();
    }



    vm.totalPayMoney=vm.totalPayMoney+number;




});


$(".offerPay_number_delete").on("touchend", function () {
    //clearTimeout(timer)
})






var storeId=$.getPara('storeId');





//获取店家优惠
function discount(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeId= storeId;
            return obj;
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api:'yg_baijiaapporder/orderPay/getOrderObject1',
        success:function(res){
            if(res.code==10000){
                var data=res.data;
                vm.discountRate=data.discountRate;
                document.title=data.storeName;
            }else{
                YDUI.dialog.toast(res.message,'error',1000)
            }

        }
    })
}




//绑定手机号
//function bindPhone() {
//    $.ajaxHandler({
//        model: function () {
//            var obj = {};
//            obj.mobile = '';
//            obj.validCode = '';
//            obj.undefinedId = JSON.parse($.getStorage('xm_uData')).openId;
//            obj.undefinedType = 2; //第三方登录类型1APP 2H5
//            return obj;
//        },
//        visibleTime: 0,
//        dataType: "json",
//        approve: true,
////       apiUrl:$.appApi(),
//        api: 'yg_baijiaapp/unDefinedUser/getUndefinedUserAdd',
//        success: function (result) {
//            if (result.code == 10000) {
//                bb = 1;
//                window.localStorage.setItem('returnWxpay', bb);
//                saveInfo(result.data);
//            } else {
//                YDUI.dialog.toast(result.message, 1000)
//            }
//
//        }
//    })
//}
   //订单号
var takeCode='';   //收货编号
//扫码支付去结算
function goBalance(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.storeId=storeId;
            obj.totalPrice=Number(vm.totalPayMoney)*100;
            obj.userId=JSON.parse($.getStorage('xm_uData')).userId;
            obj.payType=1;
            return obj
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api: 'yg_baijiaapporder/orderPay/addOrderObject1',
        success:function(result) {
            if(result.code==10000){
                var data = result.data;
                orderSn=data.result;
                takeCode=data.takeCode;
                Wxpay();

            }else if(result.code==20100){
                $.delStorage("xm_uData");
                $.delStorage("hx_wx");
                $.delStorage("hx_AccessTokenIndex");
                $.delStorage("wx");
                $.delStorage("uData");
                $.delStorage("AccessTokenIndex");
                $.GetWXCode();
            }else{
                alert(result.message)
                YDUI.dialog.toast(result.message, 1000);
            }
        }
    })
}
//微信支付
function Wxpay(){
    $.ajaxHandler({
        model:function(){
            var obj={};
            obj.orderSn=orderSn;
            obj.openId=$.getStorage('xmOpenid');
            obj.payType=3;
            console.log(obj)
            return obj
        },
        visibleTime: 0,
        dataType: "json",
        approve: true,
        api: 'yg_baijiaapporder/wecha/weChaPay',
        success:function(result) {
            if (result.code = 10000) {
                var data = result.data;
                if (data.paySign) {
                    onBridgeReady(data);
                } else {
                    //支付失败
                    _status = false;
                    YDUI.dialog.toast(result.message, 'error', 1000);
                }
            }else{
                YDUI.dialog.toast(result.message, 'error', 1000);
            }
        }
    })
}

//微信统一下单
function onBridgeReady(data){
    bb=0;
    window.localStorage.setItem('returnWxpay',bb);
    WeixinJSBridge.invoke('getBrandWCPayRequest', {
            "appId":""+data.appid+"",//公众号名称，由商户传入
            "timeStamp":""+data.timeStamp+"",//时间戳，自1970年以来的秒数
            "nonceStr":""+data.nonce_str+"", //随机串
            "package":"prepay_id="+data.prepay_id+"",
            "signType":"MD5",//微信签名方式：
            "paySign":""+data.paySign+"" //微信签名
        },
        function(res){
            // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回
            // ok，但并不保证它绝对可靠。
            //alert(res.err_msg);
            //console.log(res)
            //alert(res.err_msg);
            //$.delStorage('orderObject');
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                location.href ='/b2c/pay/wxPay_success.html?orderSn='+orderSn+'&takeCode='+takeCode+'&discountPay='+discountPay;
            }else{
                YDUI.dialog.toast('交易取消', 1000);
            }
        }
    )
}